package com.kim.nidoya.activity;

import java.util.HashMap;

import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.kim.nidoya.R;
import com.kim.nidoya.adapter.MediaCenterTitleAdapter;
import com.kim.nidoya.utils.ActivityDispatchUtil;

public class MediaCenterActivity extends BaseActivity {

	enum ViewType {
		/**
		 * 媒体中心
		 */
		MEDIACENTER,
		/**
		 * 媒体列表
		 */
		MEDIALIST,
		/**
		 * 媒体详情
		 */
		MEDIADETAILS;
	}

	private static final String[] media_category = new String[] { "课堂登入",
			"视频列表", "广告浏览", "产品浏览", "见证浏览", "学习浏览" };

	private static final String[] lessions = new String[] { "课堂20130429",
			"课堂20130430" };
	private static final String[] videos = new String[] { "视频20130429",
			"视频20130430" };
	private static final String[] adertisiments = new String[] { "广告20130429",
			"广告20130430" };
	private static final String[] products = new String[] { "产品20130429",
			"产品20130430" };
	private static final String[] lisence = new String[] { "见证20130429",
			"见证20130430" };
	private static final String[] learn = new String[] { "学习20130429",
			"学习20130430" };

	private static final HashMap<Integer, String[]> MEDIA_LIST = new HashMap<Integer, String[]>();
	static {
		MEDIA_LIST.put(0, lessions);
		MEDIA_LIST.put(1, videos);
		MEDIA_LIST.put(2, adertisiments);
		MEDIA_LIST.put(3, products);
		MEDIA_LIST.put(4, lisence);
		MEDIA_LIST.put(5, learn);
	}

	private ViewType currentViewType;

	private Button mButtonBack; // 页面返回按钮
	private ListView mListView; // 列表容器
	private WebView webView; // 网页内容容器

	private TextView mTextViewPageTitle; // 页面标题

	private MediaCenterTitleAdapter adapter;

	private String currentTitle; // 页面当前标题

	private int currentMediaCategory = -1;

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.btn_back:
			if (currentViewType.equals(ViewType.MEDIADETAILS)) {
				currentViewType = ViewType.MEDIALIST;
			} else if (currentViewType.equals(ViewType.MEDIALIST)) {
				currentViewType = ViewType.MEDIACENTER;
			}
			switchView();
			break;
		}
	}

	@Override
	protected void initParam() {
		currentViewType = ViewType.MEDIACENTER;
	}

	@Override
	protected void initView() {
		setContentView(R.layout.media_center);

		mTextViewPageTitle = (TextView) findViewById(R.id.txt_page_title);
		mButtonBack = (Button) findViewById(R.id.btn_back);
		mButtonBack.setOnClickListener(this);
		mListView = (ListView) findViewById(R.id.lst);
		webView = (WebView) findViewById(R.id.webView);

		switchView();
	}

	private void switchView() {
		switch (currentViewType) {
		case MEDIACENTER:
			mTextViewPageTitle.setText(R.string.title_media_center);
			mButtonBack.setVisibility(View.GONE);
			webView.setVisibility(View.GONE);
			mListView.setVisibility(View.VISIBLE);
			adapter = (MediaCenterTitleAdapter) mListView.getAdapter();
			if (adapter == null) {
				adapter = new MediaCenterTitleAdapter(this, media_category);
				mListView.setAdapter(adapter);
				adapter.notifyDataSetChanged();
			} else {
				adapter.refreshData(media_category);
			}
			adapter.setShowTitleLeft(false);
			mListView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					currentViewType = ViewType.MEDIALIST;
					currentMediaCategory = position;
					switchView();
				}
			});

			break;
		case MEDIALIST:
			mTextViewPageTitle.setText(media_category[currentMediaCategory]);
			mButtonBack.setVisibility(View.VISIBLE);
			webView.setVisibility(View.GONE);
			mListView.setVisibility(View.VISIBLE);
			adapter = (MediaCenterTitleAdapter) mListView.getAdapter();
			if (adapter == null) {
				adapter = new MediaCenterTitleAdapter(this,
						MEDIA_LIST.get(currentMediaCategory));
				mListView.setAdapter(adapter);
				adapter.notifyDataSetChanged();
			} else {
				adapter.refreshData(MEDIA_LIST.get(currentMediaCategory));
			}
			adapter.setShowTitleLeft(true);
			mListView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					currentViewType = ViewType.MEDIADETAILS;
					switchView();
				}
			});
			break;
		case MEDIADETAILS:
			mButtonBack.setVisibility(View.VISIBLE);
			webView.setVisibility(View.VISIBLE);
			mListView.setVisibility(View.GONE);
			break;
		}
	}

	@Override
	public void onBackPressed() {
		if (currentViewType.equals(ViewType.MEDIACENTER)) {
			ActivityDispatchUtil.toHome(this);
			return ;
		}
		if (currentViewType.equals(ViewType.MEDIADETAILS)) {
			currentViewType = ViewType.MEDIALIST;
		} else if (currentViewType.equals(ViewType.MEDIALIST)) {
			currentViewType = ViewType.MEDIACENTER;
		}
		switchView();
	}

}
